<template>
    <div id="rx-form-container">
        <div style="margin: auto;max-width: 1300px;">
          <div class="previewBox">
          <x-form-build v-if="formConfig" ref="formBuild" :dialogBox="dialogBox"
                        :form-data="formData" :global-config="globalConfig"
                        :form-config="formConfig" :form-js="formJs"
                        :contextData="contextData"
                        :permission="permission"
                        :readOnly="readonly" :alias="config.formAlias" :name="formSolution.name"
                        :id-field="idField"></x-form-build>
          </div>
        </div>
    </div>

</template>

<script>
  import {Util} from "@lowcode/jpaas-base-lib";
  import FormSolutionApi from "@/common/api/form/formSolution.js";
  import PublicApi from "@/common/api/form/public.js";
  import DialogBox from "@/assets/js/dialogBox";
  export default {
    name: "RxFormSolutionShow",
    props: ["config","record"],
    data() {
      return {
        dialogBox: DialogBox,
        formConfig:"",//控件配置
        formData:{},//表单数据
        formJs:"",//表单js
        globalConfig:"",//表单全局配置
        permission:{},//表单权限
        contextData:{},//上下文数据
        readonly:true,//是否只读
        formSolution:"",//当前表单方案
        idField:""//主键字段
      }
    },
    created() {
      this.init();
        var self=this;
        this.$bus.on("reloadExpandRow",()=>{
            self.init();
        })
    },
    methods:{
      init(){
        if(!this.config){
          return;
        }
        var self=this;
        if(this.config.isPk=='YES'){
          //主键
          var formPkMapping=Util.deepClone(this.config.formPkMapping);
          for(var i=0;i<formPkMapping.length;i++){
            var formPk=formPkMapping[i];
            if(formPk.valueSource=='param'){
              formPk.valueSource='fixedVar';
              formPk.valueDef=this.record[formPk.valueDef];
            }
          }
          this.getParams(formPkMapping,function(json){
              self.getByAlias(json.pk,{});
          })
        }else{
          //无主键
          var formMapping=Util.deepClone(this.config.formMapping);
          for(var i=0;i<formMapping.length;i++){
            var form=formMapping[i];
            if(form.valueSource=='param'){
              form.valueSource='fixedVar';
              form.valueDef=this.record[form.valueDef];
            }
          }
          this.getParams(formMapping,function(json){
              self.getByAlias(null,json);
          })
        }
      },
      getByAlias(pkId,json){
          let self_=this;
          FormSolutionApi.getByAlias(this.config.formAlias,pkId,json).then(res=> {
            let result=res;
            if(result){
              this.formSolution = result.formSolution;
              if (result.metadata) {
                let formConfig = JSON.parse(result.metadata);
                if (formConfig) {
                  result.formConfig = formConfig;
                }
                this.initForm(result);
              }
            }
          })
      },
      //初始化表单
      initForm(res) {
        if(!res.contextData){
          res.contextData={};
        }
        if (res.data.INST_ID_ ) {
          let contextDetail = {
            instId: res.data.INST_ID_
          };
          Object.assign(res.contextData, contextDetail);
        }else if(!this.pkId && this.formSolution && this.formSolution.flowDefMapping){
          //新增时获取绑定的流程设计主键
          let flows = JSON.parse(this.formSolution.flowDefMapping);
          let defId ="";
          if(flows && flows.length>0){
            defId=flows[0].defId;
          }
          let contextDetail = {
            defId: defId
          };
          Object.assign(res.contextData, contextDetail);
        }
        this.formJs =res.script;
        this.formConfig = res.formConfig;
        this.globalConfig = res.globalConfig? JSON.parse(res.globalConfig) : {};
        this.permission = res.permission;
        this.contextData = res.contextData;
        this.formData=res.data;
        this.idField=res.idField;
      },
      getParams(mapping,callback){
          PublicApi.getParams(JSON.stringify(mapping)).then(res=>{
              callback(res);
          }).catch(err=>{
              callback({});
          })
      }
    }
  }
</script>

<style scoped>

</style>
